package com.allenkerr.queue1;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;

//@Component
public class RedisTaskContainer {

  private static final Logger logger = LoggerFactory.getLogger(RedisTaskContainer.class);

  /**
   * Runtime.getRuntime().availableProcessors()
   */
  private static int runTaskThreadNum = 2;
  /**
   * 使用一个统一维护的线程池来管理隔离线程
   */
  private static ExecutorService threadPool = Executors.newFixedThreadPool(runTaskThreadNum);

  @Resource
  private RedisTemplate redisTemplate;

  public static final String ORDER_ID_QUEUE = "order:id:queue1";

  /**
   * 队列里边的数据泛型可以根据实际情况调整, 可以定义多个类似的队列
   */
  private RedisQueue1<Integer> redisQueue1 = null;

//  @PostConstruct
  private void init() {

    logger.info("----------------------------------------");
    logger.info("RedisTaskContainer.init");
    logger.info("----------------------------------------\n");

    redisQueue1 = new RedisQueue1(redisTemplate, ORDER_ID_QUEUE);

//    Consumer<Integer> consumer = new Consumer<Integer>() {
//      @Override
//      public void accept(Integer data) {
//        // do something
//        logger.info("RedisTaskContainer.accept");
//        logger.info("data = {}\n", data);
//      }
//    };
    Consumer<Integer> consumer = data -> {
      // do something
      logger.info("RedisTaskContainer.accept");
      logger.info("data = {}\n", data);
    };

    //提交线程
//    for (int i = 0; i < runTaskThreadNum; i++) {
//      threadPool.execute(
//          new OrderIdConsumer(this, consumer)
//      );
//    }
    OrderIdConsumer orderIdConsumer = new OrderIdConsumer(this, consumer);
    orderIdConsumer.start();
  }

  public RedisQueue1<Integer> getRedisQueue1() {
    return redisQueue1;
  }
}
